<%--
  Created by IntelliJ IDEA.
  User: bin.liu
  Date: 2018/12/5
  Time: 11:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:include page="/pages/sysIndex/default.jsp"/>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<style>
    .select2-selection {
        min-width: 120px;
    }
</style>

<div class="main-content">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-trophy"></i> 工单管理
            </div>
            <div class="box-tools pull-right">
                <a href="#" class="btn btn-default" id="btnSearch" title="查询"><i class="fa fa-filter"></i> 查询</a>
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_MAINTAINING_MAINTAIN_ORDER_EDIT')">
                    <a href="javascript:;" onclick="vLayuiUtil.openLayerMaxWindow('wMaintainOrder',
                            '<c:url value="/web/admin/maintainOrder/editPage.action"/>','新增一次性工单')"
                       class="btn btn-default btnTool" title="新增一次性工单"><i class="fa fa-plus"></i> 新增一次性工单</a>
                    <a href="javascript:;" onclick="downloadData()" class="btn btn-default btnTool" title="导出工单"><i
                            class="fa fa-download"></i> 导出</a>
                </sec:authorize>
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_MAINTAINING_PLANNER_ORDER_ASSIGN')">
                    <a href="javascript:;" onclick="assign(0)"
                       class="btn btn-default btnTool" title="指定接单人"><i class="fa fa-plus"></i> 指定接单人</a>
                    <a href="javascript:;" onclick="assign(1)"
                       class="btn btn-default btnTool" title="指定接单人"><i class="fa fa-plus"></i> 指定岗位</a>
                </sec:authorize>
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_MAINTAINING_MAINTAIN_ORDER_SET_DATE')">
                    <a href="javascript:;" onclick="assignDate()"
                       class="btn btn-default btnTool" title="设置截止日期"><i class="fa fa-plus"></i> 设置截止日期</a>
                </sec:authorize>
            </div>
        </div>

        <div class="box-body">
            <form id="searchForm" action="<c:url value="/web/admin/maintainOrder/listJson.action"/>" method="post"
                  class="form-inline hide" data-page-no="1" data-page-size="20" data-order-by="">
                <div class="form-group">
                    <label class="control-label">工单号：</label>
                    <div class="control-inline">
                        <input type="text" id="searchCode" name="searchCode" value="" maxlength="64"
                               class="form-control" style="width: 120px">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">工单名称：</label>
                    <div class="control-inline">
                        <input type="text" id="searchName" name="searchName" value="" maxlength="64"
                               class="form-control" style="width: 120px">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">工单类型：</label>
                    <div class="control-inline">
                        <select id="searchType" name="searchType" class="form-control">
                            <option value="" selected>全部工单</option>
                            <option value="false">周期性工单</option>
                            <option value="true">一次性工单</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">紧急程度：</label>
                    <div class="control-inline">
                        <select id="searchDegree" name="searchDegree" class="form-control">
                            <option value="">全部</option>
                            <option value="A">A</option>
                            <option value="B">B</option>
                            <option value="C">C</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">工单状态：</label>
                    <div class="control-inline">
                        <select id="searchOrderStatus" name="searchOrderStatus" class="form-control">
                            <option value="">全部</option>
                            <c:forEach items="${orderStatusArray}" var="orderStatus">
                                <option value="${orderStatus.code}">${orderStatus.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">开始时间：</label>
                    <div class="control-inline">
                        <input type="text" id="startTime" name="startTime" value="" maxlength="64"
                               class="form-control Wdate" autoComplete="off" style="width: 200px">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label">结束时间：</label>
                    <div class="control-inline">
                        <input type="text" id="endTime" name="endTime" value="" maxlength="64"
                               class="form-control Wdate" autoComplete="off" style="width: 200px">
                    </div>
                </div>

                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-sm">查询</button>
                    <button type="reset" class="btn btn-default btn-sm">重置</button>
                </div>
            </form>

            <div style="overflow-x:auto">
                <table id="dataGrid"></table>
            </div>
            <div id="dataGridPage"></div>
        </div>
    </div>
</div>
<script>
    $('#dataGrid').dataGrid({
        searchForm: $("#searchForm"),
        showCheckbox: true,
        shrinkToFit: false,
        autoScroll: true,
        columnModel: [
            <sec:authorize access="hasRole('ROLE_ENTERPRISE_MAINTAINING_MAINTAIN_ORDER_EDIT')">
            {
                header: '操作',
                name: 'actions',
                width: 250,
                align: "center",
                frozen: false,
                sortable: false,
                title: false,
                formatter: function (val, obj, row, act) {
                    var edit1 = '';
                    if (row.orderStatusEnum == 'WAIT_TAKING') {
                        edit1 = '<a href="javascript:;" class="btn btn-default btn-xs btnList" onclick=" submitCallback(\'' + row.id + '\')"><i class="fa fa-trash-o"></i>撤回</a>&nbsp;'
                    } else if (row.orderStatusEnum == 'WAIT_PULISHING') {
                        edit1 = '<a href="javascript:;" class="btn btn-default btn-xs btnList" onclick=" submitPush(\'' + row.id + '\')"><i class="fa fa-trash-o"></i>发布</a>&nbsp;'
                    }
                    // if(row.discard){
                    //     edit1 += '<a href="javascript:;" class="btn btn-default btn-xs btnList" onclick=" callbackDiscard(\'' + row.id + '\')"><i class="fa fa-trash-o"></i>取消废弃</a>&nbsp;';
                    // }else{
                    //     edit1 += '<a href="javascript:;" class="btn btn-default btn-xs btnList" onclick=" submitDiscard(\'' + row.id + '\')"><i class="fa fa-trash-o"></i>废弃</a>&nbsp;';
                    // }
                    var orderStatus='';
                    if(row.orderStatus=='已完成'){
                        orderStatus='<a  class="btn btn-default btn-xs btnList" onclick="exportExcel(' + row.id + ')" title="导出详情"><i class="fa fa-pencil"></i>导出详情</a>&nbsp;'
                    }
                    var deleteSatus='';
                <sec:authorize access="hasRole('ROLE_ENTERPRISE_MAINTAINING_MAINTAIN_ORDER_DELETE')">
                    deleteSatus= '<a href="javascript:;" class="btn btn-default btn-xs btnList" onclick=" submitRemove(\'' + row.id + '\', \'0\')"><i class="fa fa-trash-o"></i>删除</a>&nbsp;';
                    </sec:authorize>
                    return '<a  class="btn btn-default btn-xs btnList" onclick="view(' + row.id + ')" title="详情"><i class="fa fa-pencil"></i>详情</a>&nbsp;' + edit1+orderStatus+deleteSatus;
                }
            },
            </sec:authorize>
            {header: 'id', name: 'id', index: 'id', width: 150, align: "center", sortable: false},
            {header: '工单号', name: 'orderCode', index: 'orderCode', width: 150, align: "center", sortable: false},
            {header: '工单名', name: 'orderName', index: 'orderName', width: 250, align: "center", sortable: false},
            {header: '工单类型', name: 'orderType', index: 'orderType', width: 150, align: "center", sortable: false},
            {header: '紧急程度', name: 'degree', index: 'degree', width: 150, align: "center", sortable: false},
            {header: '是否指派', name: 'assignType', index: 'assignType', width: 150, align: "center", sortable: false},
            {header: '工单状态', name: 'orderStatus', index: 'orderStatus', width: 150, align: "center", sortable: false},
            {header: '开始时间', name: 'startDateTime', index: 'startDateTime', width: 250, align: "center"},
            {header: '结束时间', name: 'endDateTime', index: 'endDateTime', width: 150, align: "center", sortable: false},
            {header: '发布状态', name: 'pushStatus', index: 'pushStatus', width: 100, align: "center", sortable: false},
            {header: '发布人',name: 'pushPerson', index: 'pushPerson',    width: 100, align: "center", sortable: false},
            {header: '发布时间', name: 'pushTime', index: 'pushTime',  width: 150, align: "center", sortable: false},
            {header: '撤回人', name: 'revokePerson', index: 'revokePerson', width: 100, align: "center", sortable: false},
            {header: '撤回时间', name: 'revokeTime', index: 'revokeTime', width: 150, align: "center", sortable: false,},
            {header: '工单负责人', name: 'receiveUserName', index: 'receiveUserName', width: 100, align: "center", sortable: false},
            {header: '是否接单', name: 'receive', index: 'receive', width: 100, align: "center", sortable: false},
            {header: '接单人', name: 'receiveUserName', index: 'receiveUserName', width: 100, align: "center", sortable: false},
            {header: '接单日', name: 'receiveDate', index: 'receiveDate', width: 150, align: "center", sortable: false},
            {
                header: '开始时间',
                name: 'startDateTime',
                index: 'startDateTime',
                width: 250,
                align: "center",
                sortable: false
            },
            {header: '完成人', name: 'completeUsers', index: 'completeUsers', width: 150, align: "center", sortable: false},
            {header: '完成时间', name: 'finishTime', index: 'finishTime', width: 150, align: "center", sortable: false},
            {header: '是否完成', name: 'complete', index: 'complete', width: 100, align: "center", sortable: false},
            {header: '执行结果', name: 'result', index: 'result', width: 100, align: "center", sortable: false},
            {
                header: '实际工时(分钟)',//TODO
                name: 'totalWorkingHours',
                index: 'totalWorkingHours',
                width: 250,
                align: "center",
                sortable: false
            },
            {header: '初次审核人', name: 'firstPerson', index: 'firstPerson', width: 250, align: "center", sortable: false},
            {header: '初次审核时间', name: 'firstTime', index: 'firstTime', width: 250, align: "center", sortable: false},
            {header: '初次审核状态', name: 'firstOrder', index: 'firstOrder', width: 250, align: "center", sortable: false},
            {header: '初次审核备注', name: 'firstRemark', index: 'firstRemark', width: 250, align: "center", sortable: false},
            {header: '最终审核人', name: 'lastPerson', index: 'lastPerson', width: 250, align: "center", sortable: false},
            {header: '最终审核时间', name: 'lastTime', index: 'lastTime', width: 250, align: "center", sortable: false},
            {header: '最终审核状态', name: 'lastOrder', index: 'lastOrder', width: 250, align: "center", sortable: false},
            {header: '最终审核备注', name: 'lastRemark', index: 'lastRemark', width: 250, align: "center", sortable: false},
            {
                header: '未完成原因分类',
                name: 'cancelType',
                index: 'cancelType',
                width: 250,
                align: "center",
                sortable: false
            },
            {header: '未完成原因', name: 'cancelReason', index: 'cancelReason', width: 250, align: "center", sortable: false},
            // {
            //     header: '调整预计完成工时',
            //     name: 'endDateTime',
            //     index: 'endDateTime',
            //     width: 250,
            //     align: "center",
            //     sortable: false
            // },
            {header: '调整人', name: 'delayPerson', index: 'delayPerson', width: 250, align: "center", sortable: false},
            {header: '调整日', name: 'delayTime', index: 'delayTime', width: 250, align: "center", sortable: false},
            {header: '调整原因分类', name: 'delayType', index: 'delayType', width: 250, align: "center", sortable: false},
            {header: '调整原因', name: 'delayReason', index: 'delayReason', width: 250, align: "center", sortable: false},
            {header: '所属部门', name: 'organizationName', index: 'organizationName', width: 250, align: "center", sortable: false},
            {
                header: '所属企业',
                name: 'enterpriseName',
                index: 'enterpriseName',
                width: 250,
                align: "center",
                sortable: false
            },


        ],
        ajaxSuccess: function (data) {
            $.each(data.list, function (i, item) {
                var pushPerson;
                var pushDate;
                var person;
                var time;

                $.ajax({//发布信息
                    type: 'post',
                    url: "<c:url value="/web/admin/maintainLog/getLogInfo.action" />",
                    data: {"orderId": item.id,"type":"RELEASE_ORDER","status":"frist"},
                    dataType: 'json',
                    async: false,
                    success: function (data) {
                        pushPerson = data.data.person;
                        pushDate=data.data.timing;
                    },
                });
                $.ajax({//撤回信息
                    type: 'post',
                    url: "<c:url value="/web/admin/maintainLog/getLogInfo.action" />",
                    data: {"orderId": item.id,"type":"WITHDRAW_ORDER","status":"frist"},
                    dataType: 'json',
                    async: false,
                    success: function (data) {
                        person = data.data.person;
                        time = data.data.timing;
                    },
                });
                $("#dataGrid").jqGrid('setCell',item.id ,"pushPerson",pushPerson);
                $("#dataGrid").jqGrid('setCell',item.id ,"pushTime",pushDate);
                $("#dataGrid").jqGrid('setCell',item.id ,"revokePerson",person);
                $("#dataGrid").jqGrid('setCell',item.id ,"revokeTime",time);
            })

        }

    });
    $("#dataGrid").dataGrid('setFrozenColumns');

    function downloadData() {
        var searchCode = $('#searchCode').val();
        var searchName = $('#searchName').val();
        var searchType = $('#searchType').val();
        var searchDegree = $('#searchDegree').val();
        var searchOrderStatus = $('#searchOrderStatus').val();
        var startTime = $('#startTime').val();
        var endTime = $('#endTime').val();

        window.location.href = encodeURI(encodeURI('<c:url value="/web/admin/maintainOrder/exportOrder.action" />?searchCode=' + searchCode
            + '&searchName=' + searchName + '&searchType=' + searchType + '&searchDegree=' + searchDegree + '&searchOrderStatus=' + searchOrderStatus
            + '&startTime=' + startTime + '&endTime=' + endTime));
    }



    function view(id) {
        vLayuiUtil.openLayerIframMaxWindow('wPlannerOrder', '<c:url value="/web/admin/maintainOrder/viewPage.action?id="/>' + id, '详情', ['90%', '90%'])
    }

    function submitRemove(id) {
        js.confirm('你确认要删除这条数据吗？', function () {
            $.ajax({
                type: 'post',
                url: "<c:url value="/web/admin/maintainOrder/delete.action" />",
                data: {"id": id},
                dataType: 'json',
                async: false,
                success: function (data) {
                    js.showMessage(data.msg);
                    $('#dataGrid').dataGrid('refresh');
                },
                error: function () {
                    js.showMessage("出错了，请联系管理员");
                }
            });
        })
    }

    function exportExcel(id) {
        var ret = $("#dataGrid").jqGrid('getRowData', id);
        if(ret.orderStatus!='已完成'){
            js.showMessage("只能导出已完成的工单");
            return false;
        }
        window.location.href = "<c:url value="/web/admin/maintainOrder/exportDetailExcel.action?orderId=" />" + id;

    }

    function submitCallback(id) {
        js.confirm('你确认要撤回此工单吗？', function () {
            $.ajax({
                type: 'post',
                url: "<c:url value="/web/admin/maintainOrder/callback.action" />",
                data: {"id": id},
                dataType: 'json',
                async: false,
                success: function (data) {
                    js.showMessage(data.msg);
                    $('#dataGrid').dataGrid('refresh');
                },
                error: function () {
                    js.showMessage("出错了，请联系管理员");
                }
            });
        })
    }

    function submitPush(id) {
        js.confirm('你确认要发布此工单吗？', function () {
            $.ajax({
                type: 'post',
                url: "<c:url value="/web/admin/maintainOrder/pushUp.action" />",
                data: {"id": id},
                dataType: 'json',
                async: false,
                success: function (data) {
                    js.showMessage(data.msg);
                    $('#dataGrid').dataGrid('refresh');
                },
                error: function () {
                    js.showMessage("出错了，请联系管理员");
                }
            });
        })
    }

    function callbackDiscard(id) {
        js.confirm('你确认要取消废弃此工单吗？', function () {
            $.ajax({
                type: 'post',
                url: "<c:url value="/web/admin/maintainOrder/callbackDiscard.action" />",
                data: {"id": id},
                dataType: 'json',
                async: false,
                success: function (data) {
                    js.showMessage(data.msg);
                    $('#dataGrid').dataGrid('refresh');
                },
                error: function () {
                    js.showMessage("出错了，请联系管理员");
                }
            });
        })
    }

    function submitDiscard(id) {
        js.confirm('你确认要废弃此工单吗？', function () {
            $.ajax({
                type: 'post',
                url: "<c:url value="/web/admin/maintainOrder/submitDiscard.action" />",
                data: {"id": id},
                dataType: 'json',
                async: false,
                success: function (data) {
                    js.showMessage(data.msg);
                    $('#dataGrid').dataGrid('refresh');
                },
                error: function () {
                    js.showMessage("出错了，请联系管理员");
                }
            });
        })
    }

    //指定岗位和人员 type:0人员，1：岗位
    function assign(type) {
        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        if (ids.length == 0) {
            js.showMessage("请选择要派单的维保计划");
            return false;
        }
        if (ids.length > 1) {
            js.showMessage("只能选择一条维保计划进行派单");
            return false;
        }
        var flag = 0;
        $.each(ids, function (i, item) {
            var ret = $("#dataGrid").jqGrid('getRowData', item);
            var assignType = ret.assignType;
            console.log(assignType);
            if (type == 0 && assignType == "指派岗位") {
                flag = 1;
            }
            if (type == 1 && assignType == "指派人员") {
                flag = 2;
            }
            if (ret.orderStatus != "待发布" && ret.orderStatus != "待接单") {
                flag = 3;
            }
        });
        if (flag == 1) {
            js.showMessage("存在已经指派岗位的计划工单，不能再指派人员");
            return false;
        }
        if (flag == 2) {
            js.showMessage("存在已经指派人员的计划工单，不能再指派岗位");
            return false;
        }
        if (flag == 3) {
            js.showMessage("存在已经接单的工单，不能再进行指派操作");
            return false;
        }

        if (type == 0) {
            vLayuiUtil.openLayerIframMaxWindow('wAssignUser', '<c:url value="/web/admin/maintainOrder/assignUser.action?orderId="/>' + ids, '指定接单人')
        }
        if (type == 1) {
            vLayuiUtil.openLayerIframMaxWindow('wAssignPosition', '<c:url value="/web/admin/maintainOrder/assignPosition.action?orderId="/>' + ids, '指定岗位')
        }

    }

    //设置截止日期
    function assignDate() {
        var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
        var ret = $("#dataGrid").jqGrid('getRowData', ids);
        var orderStatus = ret.orderStatus;
        console.log(orderStatus)
        if (ids.length == 0) {
            js.showMessage("请选择要操作的工单");
            return false;
        }
        if (ids.length > 1) {
            js.showMessage("只能选择一条维保工单操作");
            return false;
        }
        if (orderStatus != "待接单" && orderStatus != "待发布") {
            js.showMessage("该工单已接单，不能再设置截止日期");
            return false;
        }
        vLayuiUtil.openLayerWindow('wAssignDate', '<c:url
                            value="/web/admin/maintainOrder/assignDatePage.action?orderId="/>' + ids, '设置截止日期');
    }

    $("#startTime").datetimepicker({
        format: 'yyyy-mm-dd hh:ii',
        minuteStep: 1,
        autoclose: true,//选中关闭
        todayBtn: true//今日按钮
    });
    $("#endTime").datetimepicker({
        format: 'yyyy-mm-dd hh:ii',
        minuteStep: 1,
        autoclose: true,//选中关闭
        todayBtn: true//今日按钮
    });

    // 时间选择器互动（结束时间不得早于开始时间）
    $("#startTime").on('changeDate', function (ev) {
        $("#endTime").datetimepicker('setStartDate', ev.date);
        var d = $("#endTime").val();
        if (d) {
            var date = new Date(d.replace(/-/g, '/'));
            if (date != 'Invalid Date' && date < ev.date) {
                $("#endTime").datetimepicker('setDate', ev.date)
            }
        }
    });

</script>
